package com.example.wordapp.controller;

import com.example.wordapp.pojo.Result;
import com.example.wordapp.pojo.User;
import com.example.wordapp.pojo.Word;
import com.example.wordapp.service.WordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/word")
public class WordController {
    @Autowired
    private WordService wordService;

    @RequestMapping("/list")
    // 获取所有单词
    public Result list(HttpServletRequest request) {
        log.info("查询所有单词列表");
        return Result.success(wordService.getAll());
    }

    // 获取该用户没有背过的30个单词
    @RequestMapping("/wirte")
    public Result getWords(HttpServletRequest request, @RequestBody User user) {
        log.info("查询该用户没有被过的单词 name: {}, id: {}", user.getName(), user.getId());
        List<Word> words = wordService.getByUserFormart(user);
        return Result.success(words);
    }

    // 修改用户背过的单词的接口
    @RequestMapping("/update")
    public Result setWordStatus(HttpServletRequest request, String userId, String wordId) {
        log.info("修改已背的单词状态 用户: {}  单词: {}", userId, wordId);
        int row = wordService.updateWriteWord(userId, wordId, 1);
        return Result.success();
    }

    // 移除用户背过的单词的接口
    @RequestMapping("/remove")
    public Result removeWordStatus(HttpServletRequest request, String userId, String wordId) {

        log.info("移除已背的单词状态 用户: {}  单词: {}", userId, wordId);
        int row = wordService.updateWriteWord(userId, wordId, 0);
        return Result.success();
    }

    // 所有已背单词
    @RequestMapping("/findWriteAll/{userId}")
    public Result allWriteWord(HttpServletRequest request, @PathVariable String userId) {
        log.info("查询已背单词用户 id {} ", userId);
        List<Word> wordList = wordService.findWriteWordByCom(userId, 1);
        return Result.success(wordList);
    }

    @RequestMapping("/clean")
    public Result cleanAll(HttpServletRequest request, @RequestBody User user) {
        log.info("清空所有已背 id {}  密码{}", user.getId(), user.getCode());
        int row = wordService.cleanWriterWords(user.getId().toString(), user.getCode());
        if (row == -1) {
            return Result.error("密码错误");
        }
        return Result.success();
    }
}
